package com.example.neusoft.yuejian.Fragment;

import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.neusoft.yuejian.Activity.ActDetailActivity;
import com.example.neusoft.yuejian.Activity.MatchDetailActivity;
import com.example.neusoft.yuejian.Activity.SowingDetailActivity;
import com.example.neusoft.yuejian.Base.BaseFragment;
import com.example.neusoft.yuejian.Base.BaseListener;
import com.example.neusoft.yuejian.Base.BaseModel;
import com.example.neusoft.yuejian.Base.URL;
import com.example.neusoft.yuejian.Bean.ActBean;
import com.example.neusoft.yuejian.Bean.MasterBean;
import com.example.neusoft.yuejian.Bean.MatchBean;
import com.example.neusoft.yuejian.Bean.SoWing;
import com.example.neusoft.yuejian.R;
import com.squareup.picasso.Picasso;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


import retrofit2.Call;


public class ShouyeFragment extends BaseFragment implements OnBannerListener {

    private List<MatchBean> lists;
    private List<MasterBean> lists1;
    private List<ActBean> lists2;
    private List<SoWing> list5;
    RecyclerView jchd;
    RecyclerView qwds;
    RecyclerView zjbs;

    private Banner banner;
    private ArrayList<String> list_path;
    private ArrayList<String> list_title;

    private SharedPreferences sp;

//轮播监听
    @Override
    public void OnBannerClick(int position) {
        Toast.makeText(getActivity(), "你点了第" + (position + 1) + "张轮播图", Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(getActivity(), SowingDetailActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("sowing", list5.get(position));
            intent.putExtras(bundle);
            startActivity(intent);

    }

    /**
     * 网络加载图片
     * 使用了Glide图片加载框架
     */
    private class MyLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context.getApplicationContext())
                    .load((String) path)
                    .into(imageView);
        }
    }


    @Override
    public int getLayoutFile() {
        return R.layout.fragment_shouye;
    }

    @Override
    public void initView() {
        jchd =  view.findViewById(R.id.jchd);
        jchd.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        qwds =  view.findViewById(R.id.qwds);
        qwds.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        zjbs =  view.findViewById(R.id.zjbs);
        zjbs.setLayoutManager(new LinearLayoutManager(this.getActivity()));

        banner = view.findViewById(R.id.banner);
        list_path = new ArrayList<>();
        list_title = new ArrayList<>();
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        banner.setImageLoader(new MyLoader());
        banner.setBannerAnimation(Transformer.Default);
        banner.setBannerTitles(list_title);
        banner.setDelayTime(2000);
        banner.isAutoPlay(true);
        banner.setOnBannerListener(this);

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {

        getLunBo();
        getData();
        getActdata();
        getmasterdata();
    }
    public void getLunBo(){
        BaseModel<SoWing> model=new BaseModel<>();
        Call<List<SoWing>> call=model.service.getPic();
        model.callListEnqueue(call,linerset);
    }

    BaseListener<List<SoWing>> linerset=new BaseListener<List<SoWing>>() {
        @Override
        public void onResponse(List<SoWing> soWings) {
            list5=soWings;
            for(int i=0;i<list5.size();i++){
                list_path.add("http://10.0.2.2/YueJian/public/static/img/showimg/"+list5.get(i).getSowingpic());
                list_title.add(list5.get(i).getSowingtitle());
            }

            banner.setImages(list_path)
                    .setBannerTitles(list_title)
                    .start();
            banner.updateBannerStyle(BannerConfig.NUM_INDICATOR_TITLE);

        }

        @Override
        public void onFail(String message) {
            showToast("123");
        }
    };

    private void getActdata() {
        BaseModel<ActBean> model = new BaseModel<>();
        Call<List<ActBean>> call = model.service.actList();
        model.callListEnqueue(call, listener);
    }

    public void getmasterdata() {
        BaseModel<MasterBean> model = new BaseModel<>();
        Call<List<MasterBean>> call = model.service.masterlist();
        model.callListEnqueue(call, list1);
    }

    public void getData() {
        BaseModel<MatchBean> model = new BaseModel<>();
        Call<List<MatchBean>> call = model.service.macthlist();
        model.callListEnqueue(call, list);
    }


    BaseListener<List<MatchBean>> list=new BaseListener<List<MatchBean>>() {
        @Override
        public void onResponse(List<MatchBean> matchBeans) {

            lists=matchBeans;
            zjbs.setAdapter(new GameAdapter(lists));

        }

        @Override
        public void onFail(String msg) {
            showToast("111");
        }
    };
   BaseListener<List<MasterBean>> list1=new BaseListener<List<MasterBean>>() {
       @Override
       public void onResponse(List<MasterBean> masterBeans) {
       lists1=masterBeans;
       qwds.setAdapter(new MasterAdapter(lists1));
       }
       @Override
       public void onFail(String msg) {
           showToast(msg);
       }
   };
   BaseListener<List<ActBean>> listener=new BaseListener<List<ActBean>>() {
       @Override
       public void onResponse(List<ActBean> actBeans) {
           lists2=actBeans;
           jchd.setAdapter(new ActAdapter(lists2));
       }

       @Override
       public void onFail(String msg) {
           showToast("111");
       }
   };






    public class GameAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private List<MatchBean> matchBean;
        private MatchBean list;

        public GameAdapter( List<MatchBean> mDataList){

            this.matchBean = mDataList;


        }
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater=LayoutInflater.from(getActivity());
            View v = inflater.inflate(R.layout.gamelist_item, parent, false);
            return new ViewHolder(v);
        }


        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
            list= matchBean.get(position) ;
            if (null == list)
                return;
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.game_name.setText(list.getMname());
            Picasso.with(getActivity()).load("http://10.0.2.2/YueJian/public/static/img/"+
                    list.getMpic()).into(viewHolder.game_pic);
            viewHolder.gl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(getActivity(), MatchDetailActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putSerializable("mid",(Serializable) list.getMid());
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return matchBean.size();
        }

        private class ViewHolder extends RecyclerView.ViewHolder {
            ImageView game_pic;
            TextView game_name;
            private LinearLayout gl;
            private ViewHolder(View v) {
                super(v);
                game_pic =  v.findViewById(R.id.game_pic);
                game_name =  v.findViewById(R.id.game_name);
                gl=v.findViewById(R.id.gl);
            }
        }
    }



    public class MasterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private MasterBean list;
        private List<MasterBean> masterBean;

        public MasterAdapter( List<MasterBean> mDataList){

            this.masterBean = mDataList;


        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(getActivity());
            View v = inflater.inflate(R.layout.gamelist_item, parent, false);
            return new ViewHolder(v);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            list= masterBean.get(position) ;
            if (null == list)
                return;
           ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.game_name.setText(list.getMastername());
            Picasso.with(getActivity()).load("http://10.0.2.2/YueJian/public/static/img/" +
                    list.getMasterpic()).into(viewHolder.game_pic);
        }

        @Override
        public int getItemCount() {
            return masterBean.size();
        }
    }
        private class ViewHolder extends RecyclerView.ViewHolder {
           private TextView game_name;
           private ImageView game_pic;
            public ViewHolder(View v) {
                super(v);
                game_name =  v.findViewById(R.id.game_name);
                game_pic = v.findViewById(R.id.game_pic);

            }
        }
    public class ActAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private List<ActBean> actBean;
        private ActBean list;


        public ActAdapter( List<ActBean> mDataList){

            this.actBean = mDataList;


        }
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater=LayoutInflater.from(getActivity());
            View v = inflater.inflate(R.layout.gamelist_item, parent, false);
            return new ViewHolder(v);
        }


        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            list = actBean.get(position);
            if (null == list)
                return;
            ViewHolder viewHolder = (ViewHolder) holder;
            Picasso.with(getActivity()).load("http://10.0.2.2/YueJian/public/static/img/"
                    + list.getApic()).into(viewHolder.game_pic);
            viewHolder.game_name.setText(list.getAname());
            viewHolder.gl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), ActDetailActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("aid",(Serializable) list.getAid());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
        @Override
        public int getItemCount() {
            return actBean.size();
        }

        private class ViewHolder extends RecyclerView.ViewHolder {
            ImageView game_pic;
            TextView game_name;
            LinearLayout gl;
            public ViewHolder(View v) {
                super(v);
                game_pic =  v.findViewById(R.id.game_pic);
                game_name =  v.findViewById(R.id.game_name);
                gl=v.findViewById(R.id.gl);
            }
        }
    }

    }



